package jade.ui;

import jade.Main;
import jade.ia.Moteur;
import jade.ia.Moteur.Reponse;
import jade.ia.MoteurSeries;
import jade.ia.MoteurSports;
import jade.metier.ControleurFactory.TypeEntite;
import java.util.Random;

/**
 * Fenêtre : Inférence en cours
 */
public class Jeu extends javax.swing.JFrame {

    // Moteur d'inférence
    private Moteur moteur = null;

    // Numéro de la question
    private int numQuestion;

    /** Creates new form Jeu */
    public Jeu(TypeEntite e) {
        initComponents();
        this.setLocationRelativeTo(this.getParent());
        
        initialiserJeu(e);
  
    }

    /**
     * Initialisation du moteur
     * @param e Entitée à deviner
     */
    private void initialiserJeu(TypeEntite e)
    {
        switch(e){
            case SERIE:
                moteur = new MoteurSeries();
            break;
            case SPORT:
                moteur = new MoteurSports();
            break;
        }

        afficherQuestion(moteur.getProchaineQuestion());
        numQuestion = 1;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnOui = new javax.swing.JButton();
        btnNon = new javax.swing.JButton();
        btnBof = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtQuestion = new javax.swing.JTextPane();
        txtNumQuestion = new javax.swing.JLabel();
        imgRobot = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Laissez moi deviner");
        setAlwaysOnTop(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        btnOui.setText("Oui");
        btnOui.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnOuiMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnOuiMouseReleased(evt);
            }
        });
        btnOui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOuiActionPerformed(evt);
            }
        });

        btnNon.setText("Non");
        btnNon.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnNonMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnNonMouseReleased(evt);
            }
        });
        btnNon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNonActionPerformed(evt);
            }
        });

        btnBof.setText("Je ne sais pas");
        btnBof.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnBofMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnBofMouseReleased(evt);
            }
        });
        btnBof.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBofActionPerformed(evt);
            }
        });

        txtQuestion.setBorder(null);
        txtQuestion.setContentType("text/html");
        txtQuestion.setEditable(false);
        txtQuestion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        txtQuestion.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        txtQuestion.setFocusCycleRoot(false);
        txtQuestion.setFocusable(false);
        txtQuestion.setHighlighter(null);
        txtQuestion.setVerifyInputWhenFocusTarget(false);
        jScrollPane1.setViewportView(txtQuestion);

        txtNumQuestion.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
        txtNumQuestion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtNumQuestion.setText("1");

        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic.png"))); // NOI18N
        imgRobot.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                imgRobotMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                imgRobotMouseReleased(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
        jLabel2.setText("Question numéro : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imgRobot, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtNumQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnOui, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnNon, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnBof, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNumQuestion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOui, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNon, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBof, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(imgRobot, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        Main.getFenAccueil().setEnabled(true);
    }//GEN-LAST:event_formWindowClosing

    private void btnOuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOuiActionPerformed
       action(Reponse.OUI);
    }//GEN-LAST:event_btnOuiActionPerformed

    private void btnNonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNonActionPerformed
        action(Reponse.NON);
    }//GEN-LAST:event_btnNonActionPerformed

    private void btnBofActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBofActionPerformed
        action(Reponse.BOF);
    }//GEN-LAST:event_btnBofActionPerformed


    //
    // ----------- Décoration --------------------------------------------------------------------------------------
    //

    private void btnOuiMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOuiMousePressed
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic1.png")));
    }//GEN-LAST:event_btnOuiMousePressed

    private void btnOuiMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOuiMouseReleased
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic.png")));
    }//GEN-LAST:event_btnOuiMouseReleased

    private void btnNonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNonMousePressed
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic3.png")));
    }//GEN-LAST:event_btnNonMousePressed

    private void btnNonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNonMouseReleased
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic.png")));
    }//GEN-LAST:event_btnNonMouseReleased

    private void btnBofMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnBofMousePressed
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic2.png")));
    }//GEN-LAST:event_btnBofMousePressed

    private void btnBofMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnBofMouseReleased
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic.png")));
    }//GEN-LAST:event_btnBofMouseReleased

    private void imgRobotMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgRobotMousePressed
        Random r = new Random();
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic"+(r.nextInt(3)+1)+".png")));
    }//GEN-LAST:event_imgRobotMousePressed

    private void imgRobotMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgRobotMouseReleased
        imgRobot.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/robot_clic.png")));
    }//GEN-LAST:event_imgRobotMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBof;
    private javax.swing.JButton btnNon;
    private javax.swing.JButton btnOui;
    private javax.swing.JLabel imgRobot;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel txtNumQuestion;
    private javax.swing.JTextPane txtQuestion;
    // End of variables declaration//GEN-END:variables

//
// ----------- Fin Décoration ----------------------------------------------------------------------------
//


    /**
     * Le joueur envoie sa réponse au moteur
     * @param rep
     */
    private void action(Reponse rep)
    {
        moteur.envoyerReponse(rep);
        if (moteur.peutPoserQuestion()) {
            afficherQuestion(moteur.getProchaineQuestion());
            incrementerNumeroQuestion();
        } else {
            afficherProposition();
        }
    }

    /**
     * Mettre à jour la question affichée
     * @param question
     */
    private void afficherQuestion(String question)
    {
        this.txtQuestion.setText("<div style='text-align:center; font-weight:bold; font-size:12px; font-family: Tahoma'>"+question+"</div>");
    }

    /**
     * Incrémenter le numéro de la question affichée
     */
    private void incrementerNumeroQuestion()
    {
        numQuestion++;
        this.txtNumQuestion.setText(""+numQuestion);
    }

    /**
     * Afficher une proposition de résultat
     */
    private void afficherProposition()
    {
        Proposition fenProposition = new Proposition(moteur);
        fenProposition.setVisible(true);
        this.setVisible(false);
    }
}
